package com.codeoss.framework.ureport2;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bstek.ureport.provider.report.ReportFile;
import com.bstek.ureport.provider.report.ReportProvider;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.module.ir.model.IrReport;
import com.codeoss.module.ir.service.IIrReportService;
@Service
public class YunReportProvider implements ReportProvider {
	private String prefix="db:";
	@Autowired
	private IIrReportService irReportService;

	@Override
	public InputStream loadReport(String file) {
		// TODO Auto-generated method stub
		QueryWrapper<IrReport> query = new QueryWrapper<IrReport>();
		if(file.startsWith(prefix)){
			file=file.substring(prefix.length(),file.length());
		}
		query.eq("code", file);
		IrReport irReport = irReportService.getOne(query);
		String jsonContent=irReport.getJsonContent();
		if(!StringUtils.isEmpty(jsonContent)) {
			ByteArrayInputStream is=new ByteArrayInputStream(jsonContent.getBytes());
			return is;
		}else {
			query = new QueryWrapper<IrReport>();
			query.eq("code", "common.ureport.xml");
			irReport = irReportService.getOne(query);
			jsonContent=irReport.getJsonContent();
			ByteArrayInputStream is=new ByteArrayInputStream(jsonContent.getBytes());
			return is;
		}
	}

	@Override
	public void deleteReport(String file) {
		// TODO Auto-generated method stub
		QueryWrapper<IrReport> query = new QueryWrapper<IrReport>();
		if(file.startsWith(prefix)){
			file=file.substring(prefix.length(),file.length());
		}
		query.eq("code", file);
		IrReport irReport = irReportService.getOne(query);
		irReportService.delete(irReport);
	}

	@Override
	public List<ReportFile> getReportFiles() {
		// TODO Auto-generated method stub
		List<ReportFile> list=new ArrayList<ReportFile>();
		List<IrReport> reportList=irReportService.list();
		for (IrReport irReport:reportList) {
			ReportFile file=new ReportFile(irReport.getCode(),new Date());
			list.add(file);
		}
		return list;
	}

	@Override
	public void saveReport(String file, String content) {
		// TODO Auto-generated method stub
		QueryWrapper<IrReport> query = new QueryWrapper<IrReport>();
		if(file.startsWith(prefix)){
			file=file.substring(prefix.length(),file.length());
		}
		query.eq("code", file);
		IrReport irReport = irReportService.getOne(query);
		if (irReport == null) {
			irReport = new IrReport();
			irReport.setCode(file);
		}
		irReport.setJsonContent(content);
		if (irReport.getId() == null) {
			irReportService.insert(irReport);
		} else {
			irReportService.update(irReport);
		}
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "数据库";
	}

	@Override
	public boolean disabled() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String getPrefix() {
		// TODO Auto-generated method stub
		return "db:";
	}

}
